/**
 * @author Rudy Kaplan Depena (The University of Texas at Austin)
 */
package com.utaustin.mirankerlab.ultrawrap.compiler.expressionTree.sparql;

/** A Special Flavor of Join that Left Joins two Operands */
public class LeftJoin extends Join {

	/**
	 * String representation of LeftJoin
	 * @param String
	 */
	public String toString(){
		return "LeftJoin (" + this.getLeftChild()+  ", " + this.getRightChild() + ")";
	}
	
	/**
	 * Generate SQL Node
	 * @return sql representation of node
	 */
	public String generateSql(boolean calledByLeftJoin){
		String left = this.getLeftChild().generateSql(true);
		String right = this.getRightChild().generateSql(true);
		
		//if(right.contains("ON") && this.getRightChild() instanceof BGP)
		if(this.getRightChild() instanceof BGP)
		{
			BGP rightBGP = (BGP) this.getRightChild();
			if(rightBGP.getTriples().size() == 1)
			{
				return "" + this.getLeftChild().generateSql(true) + "" + "\n LEFT OUTER JOIN \n"
				+ "" + this.getRightChild().generateSql(true);
			}
		}
/*		else
		{
			return "" + this.getLeftChild().generateSql(true) + "" + "\n LEFT OUTER JOIN \n"
			+ "" + this.getRightChild().generateSql(true) + "" + " \n ON (1=1)";
		}*/
		
		return "" + this.getLeftChild().generateSql(true) + "" + "\n LEFT OUTER JOIN \n"
		+ "" + this.getRightChild().generateSql(true) + "" + " \n ON (1=1)";
		
	}

}